;
(function($,win)
{
	/* @------------------------------------------------------------------------------------@ */
	$(document).ready(function()
	{
		var url = location.search;
		if (url.indexOf("?adinfoId=") != -1) {
			var reqPath = basePath+'adinfoManager/queryadinfoById.action'+url;
			var sendBody = '{}';
			send(reqPath, "POST", "application/json", "json", sendBody, function(res)
			{
				if (res.result == 'success')
				{
					if (res.data != null)
					{
						var adinfo = res.data;
						$("#uu_id").val(adinfo.uuid);
						$("#adinfo_name").val(adinfo.adinfoName);
						$("#nick_name").val(adinfo.nickName);
						$("#adinfo_introduce").val(adinfo.adinfoIntroduce);
					} else
					{
						alert("没有查找到相关数据");
					}
				}
			});
		}else
		{
			//初始化列表
			iniadinfoData(0,15);
		}
		$('#addnew').click(function()
		{

			window.location.href = jumpPath + 'view/technology/adinfo/addadinfo.jsp';
		});

		// 查询
		$('#btn_search').bind('click', function()
		{
			iniadinfoData(0,15);
		});

		$('#tbn_save').bind('click', function()
		{
			saveadinfo();
		});
		
		$('#tbn_update').bind('click', function()
		{
			updateadinfo();
		});

	});// the end

	/* @-----------------------------------------------------------------------------------------@ */
})(jQuery, window);

function editadinfo(adinfoId)
{
	window.location.href = jumpPath + 'view/technology/adinfo/editadinfo.jsp?adinfoId='+adinfoId;
}

function del(id)
{

	if (confirm("确定要删除吗？"))
	{
		var reqPath = basePath+'adManager/deleteAd.action?adId='+id;
		send(reqPath, "POST", "application/json", "json", null, function(res)
		{
			if (res.result == 'success')
			{
				iniadinfoData(0,15);
			}
		});
	}
}

function iniadinfoData(pageNumber,pagesize)
{

	var reqPath = basePath+'adManager/queryAdByPage.action';
	var sendBody = '{"currentPage": 1,"pageSize": 15}';
	var $tab = $('#adinfoDataTables tbody').html("");

	
	var reqPath = basePath + 'adManager/queryAdByPage.action';
	var sendBody = '{"currentPage": '+pageNumber+',"pageSize": '+pagesize+'}';
	var $tab = $('#adinfoDataTables tbody').html("");

	send(reqPath, "POST", "application/json", "json", sendBody, function(res)
	{

		if (res.result == 'success')
		{

			if (res.data.data.length > 0)
			{
				var tpl_tr = $('#tpl_list').html(), rows = '';

				$.each(res.data.data, function(i,n)
				{
					var tpye ='';
					if(n.type == 0)
					{
						type='活动资讯';
					}else if (n.type == 1)
					{
						type='滑屏广告';
					}else if (n.type == 2)
					{
						type='词条广告';
					}
					var operation = "<button onclick='del(\""+n.uuid+"\")' type='button' class='btn btn-primary'>删除</button>";
					if(n.isClose == 'N')
					{
						operation += " <button onclick='closeAdinfo(\""+n.uuid+"\")' type='button' class='btn btn-primary'>关闭</button>";
					}else if (n.isClose == 'Y')
					{
						operation += " <button onclick='openAdinfo(\""+n.uuid+"\")' type='button' class='btn btn-primary'>打开</button>";
					}
					rows += tpl_tr.replace(/\$type/g, type)
							.replace(/\$businessName/,n.businessName)
							.replace(/\$userQq/, n.userQq)
							.replace(/\$phone/, n.phone)
							.replace(/\$telephone/, n.telephone)
							.replace(/\$userAddress/, n.userAddress)
							.replace(/\$businessScope/, n.businessScope)
							.replace(/\$businessDescription/, n.businessDescription)
							.replace(/\$operation/, operation);
				});
				rows = rows.replace(/null/gm,"");
				$tab.append(rows);
				// page
				pagefunction(iniadinfoData,res.data.totalPages,pagesize);

			} else
			{
				$tab.append('<tr><td colspan="7">没有查找到相关数据。</td></tr>');
			}
		}
	});

}

function closeAdinfo(id)
{
	var requsetPath = basePath + "adManager/updatead.action";
	var sendBody = '{"uuid": "'+id+'","isClose": "Y"}';
	send(requsetPath, "POST", "application/json", "json", sendBody,
			function(resultData)
			{
				if (resultData.result == 'success')
				{
					// 跳转到列表
					window.location = jumpPath
							+ 'view/technology/adinfo/adinfolist.jsp';
				} else
				{
					alert(resultData.errorMsg);
				}
			});
}

function openAdinfo(id)
{
	var requsetPath = basePath + "adManager/updatead.action";
	var sendBody = '{"uuid": "'+id+'","isClose": "N"}';
	send(requsetPath, "POST", "application/json", "json", sendBody,
			function(resultData)
			{
				if (resultData.result == 'success')
				{
					// 跳转到列表
					window.location = jumpPath
							+ 'view/technology/adinfo/adinfolist.jsp';
				} else
				{
					alert(resultData.errorMsg);
				}
			});
}

/**
 * 
 * @METHODNAME: saveLoginadinfo
 * @METDHOD:
 * @Description: 添加登陆用户
 * @author Administrator
 * @date 2015年1月10日 下午5:37:52
 */
function saveadinfo()
{
	
	var requsetPath = basePath + "adManager/insertAd.action";
	var requestData = $('#frm_login_adinfo').serializeObject();
	requestData.imgUrl = $('input[name="imgUrl"]').val();
	requestData = JSON.stringify(requestData);
	// alert(requestData);
	send(requsetPath, "POST", "application/json", "json", requestData,
			function(resultData)
			{

				if (resultData.result == 'success')
				{
					// 跳转到列表
					window.location = jumpPath
							+ 'view/technology/adinfo/adinfolist.jsp';
				} else
				{
					alert(resultData.errorMsg);
				}
			});

}


function updateadinfo()
{
	
	var requsetPath = basePath + "adinfoManager/updateadinfo.action";
	var requestData = $('#frm_login_adinfo').serializeObject();
	requestData = JSON.stringify(requestData);
	// alert(requestData);
	send(requsetPath, "POST", "application/json", "json", requestData,
			function(resultData)
			{

				if (resultData.result == 'success')
				{
					// 跳转到列表
					window.location = jumpPath
							+ 'view/technology/adinfo/adinfolist.jsp';
				} else
				{
					alert(resultData.errorMsg);
				}
			});

}
